package io.openinstall.unity;

import com.fm.openinstall.listener.*;
import android.util.*;
import com.unity3d.player.*;
import com.fm.openinstall.model.*;
import org.json.*;

public class OiWakeupCallback extends AppWakeUpAdapter
{
    private static boolean register = false;
    private static final String wakeupCallbackMethod = "_wakeupCallback";
    private static final String wakeupCallbackObject = "OpenInstall";
    private static String wakeupDataHolder;

    public static void registerWakeup() {
        final String wakeupDataHolder = OiWakeupCallback.wakeupDataHolder;
        if (wakeupDataHolder != null) {
            sendUnityMessage(wakeupDataHolder);
            OiWakeupCallback.wakeupDataHolder = null;
        }
        OiWakeupCallback.register = true;
    }

    public static void sendUnityMessage(final String s) {
        Log.d("OpenInstall", String.format("OiInstallCallback UnitySendMessage(%s, %s, %s)", "OpenInstall", "_wakeupCallback", s));
        UnityPlayer.UnitySendMessage("OpenInstall", "_wakeupCallback", s);
    }

    @Override
    public void onWakeUp(final AppData appData) {
        final JSONObject jsonObject = new JSONObject();
        while (true) {
            try {
                jsonObject.put("channelCode", (Object)appData.getChannel());
                jsonObject.put("bindData", (Object)appData.getData());
                final String string = jsonObject.toString();
                if (OiWakeupCallback.register) {
                    sendUnityMessage(string);
                    return;
                }
                OiWakeupCallback.wakeupDataHolder = string;
            }
            catch (Exception ex) {
                continue;
            }
            break;
        }
    }
}
